<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model{
	function __construct() {
		parent::__construct();
	}
	
	function check_login($nim,$pwd){
		$clause = array(
			'nim'=>$nim,
			'password'=>md5($pwd)
		);
		$query_login = $this->db->get_where('user',$clause);
		if($query_login->num_rows()>0){
			return $query_login->row_array();
		}else{
			return FALSE;
		}
	}
	
	function new_user($data){
		//cek apakah nim tsb sudah ada
		$cek_nim = $this->db->get_where('user',array(
			'nim'=>$data['nim']
		));
		if($cek_nim->num_rows>0){
			return FALSE;
		}else{
			$data['password']=md5($data['password']);
			$this->db->insert('user',$data);
			return TRUE;
		}
	}
	
	function update_user($data){
		$nim = $data['nim'];
		unset ($data['nim']);
		$this->db->where('nim',$nim);
		$this->db->update('user',$data);
	}
	
	function get_user($nim){
		$query = $this->db->get_where('user',array('nim'=>$nim));
		if($query->num_rows>0){
			return $query->row_array();
		}else{
			return FALSE;
		}
	}
	
	function get_all_user(){
		$query = $this->db->get('user');
		return $query->result_array();
	}
	
	function delete_user($nim){
		$this->db->delete('user',array('nim'=>$nim));
	}
	
	function nilai_pti_b($nim,$nilai){
		$data = array('nilai_pti_b'=>$nilai);
		$this->db->where('nim',$nim);
		$this->db->update('user',$data);
		$this->session->set_userdata($data);
	}
	function nilai_pti_c($nim,$nilai){
		$data = array('nilai_pti_c'=>$nilai);
		$this->db->where('nim',$nim);
		$this->db->update('user',$data);
		$this->session->set_userdata($data);
	}
}

/** End of file */